<!DOCTYPE HTML PUBLIC "-//ORA//DTD CD HTML 3.2//EN">
<HTML>
<HEAD>
<TITLE>[Chapter 1] Introduction</TITLE>
<META NAME="author" CONTENT="Mark Grand">
<META NAME="date" CONTENT="Thu Jul 31 13:05:14 1997">
<META NAME="form" CONTENT="html">
<META NAME="metadata" CONTENT="dublincore.0.1">
<META NAME="objecttype" CONTENT="book part">
<META NAME="otheragent" CONTENT="gmat dbtohtml">
<META NAME="publisher" CONTENT="O'Reilly &amp; Associates, Inc.">
<META NAME="source" CONTENT="SGML">
<META NAME="subject" CONTENT="java">
<META NAME="title" CONTENT="Java Language Reference">
<META HTTP-EQUIV="Content-Script-Type" CONTENT="text/javascript">
</HEAD>
<body vlink="#551a8b" alink="#ff0000" text="#000000" bgcolor="#FFFFFF" link="#0000ee">

<DIV CLASS=htmlnav>
<H1><a href='index.htm'><IMG SRC="gifs/smbanner.gif"
     ALT="Java Language Reference" border=0></a></H1>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch00_07.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><B><FONT FACE="ARIEL,HELVETICA,HELV,SANSERIF" SIZE="-1">Chapter 1</FONT></B></TD>
<td width=172 align=right valign=top><A HREF="ch01_02.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
</table>

&nbsp;
<hr align=left width=515>
</DIV>
<H1 CLASS=chapter><A CLASS="TITLE" NAME="JLR2-CH-1">1. Introduction</A></H1>

<DIV CLASS=htmltoc>

<p>
<b>Contents:</b><br>
A "Hello World" Program<br>
<A HREF="ch01_02.htm">New Language Features in Java 1.1</A><BR>
<A HREF="ch01_03.htm">Compiling a Java Source File</A><BR>
<A HREF="ch01_04.htm">Running a Java Application</A><BR>
<A HREF="ch01_05.htm">Notational Conventions</A><BR>

<p>
</DIV>

<P CLASS=para>
Java is a relatively new programming language. However, many of the
features that make up the language are not new at all. Java's
designers borrowed features from a variety of older languages, such as
Smalltalk and Lisp, in order to achieve their design goals.

<P CLASS=para>
Java is designed to be both robust and secure, so that it can be used
to write small, hosted programs, or <I CLASS=emphasis>applets</I>,
that can be run safely by
hosting programs such as Web browsers and cellular phones. Java also
needs to be portable, so that these programs can run on many different
kinds of systems. What follows is a list of the important features
that Java's designers included to create a robust, secure, and
portable language.

<P>
<UL CLASS=itemizedlist>
<li CLASS=listitem>Java is a simple language. It borrows most of its syntax from
C/C++, so it is easy for C/C++ programmers to understand
the syntax of Java code.
But that is where the similarities end. Java does not support
troublesome features from C/C++, so it is much simpler than either of
those languages. In fact, if you examine the features of Java, you'll
see that it has more in common with languages like Smalltalk and Lisp.

<P>
<li CLASS=listitem>Java is a statically typed language, like C/C++. This means that
the Java compiler can perform static type checking and enforce a
number of usage rules.

<P>
<li CLASS=listitem>Java is fully runtime-typed as well. The Java runtime system
keeps track of all the objects in the system, which makes it possible
to determine their types at runtime. For example, casts from one
object type to another are verified at runtime. Runtime typing also
makes it possible to use completely new, dynamically loaded objects
with some amount of type safety.

<P>
<li CLASS=listitem>Java is a late-binding language, like Smalltalk, which means that it
binds method calls to their definitions at runtime. Runtime binding is
essential for an object-oriented language, where a subclass can
override methods in its superclass, and only the runtime system can
determine which method should be invoked. However, Java also supports
the performance benefits of early binding. When the compiler can
determine that a method cannot be overridden by subclassing, the
method definition is bound to the method call at compile-time.

<P>
<li CLASS=listitem>Java takes care of memory management for applications, which is
unlike C/C++, where the programmer is responsible for explicit memory
management. Java supports the dynamic allocation of arrays and
objects, and then takes care of reclaiming the storage for objects and
arrays when it is safe to do so, using a technique called <I CLASS=emphasis>garbage
collection</I>. This eliminates one of the largest sources of bugs in
C/C++ programs.

<P>
<li CLASS=listitem>Java supports object references, which are like pointers in C/C++.
However, Java does not allow any manipulation of references. For
example, there is no way that a programmer can explicitly dereference
a reference or use pointer arithmetic. Java implicitly handles
dereferencing references, which means that they can be used to do
most of the legitimate things that C/C++ pointers can do.

<P>
<li CLASS=listitem>Java uses a single-inheritance class model, rather than the
error-prone multiple-inheritance model used by C++. Instead, Java
provides a feature called an <I CLASS=emphasis>interface</I>
(borrowed from Objective C) that specifies the behavior of
an object without defining
its implementation. Java supports multiple inheritance of interfaces,
which provides many of the benefits of multiple inheritance, without
the associated problems.

<P>
<li CLASS=listitem>Java has support for multiple threads of execution built into the
language, so there are mechanisms for thread synchronization and
explicit waiting and signaling between threads.

<P>
<li CLASS=listitem>Java has a powerful exception-handling mechanism, somewhat like that
in newer implementations of C++. Exception handling provides a way to
separate error-handling code from normal code, which leads to cleaner,
more robust applications.

<P>
<li CLASS=listitem>Java is both a compiled and an interpreted language. Java code is
compiled to Java byte-codes, which are then executed by a Java
runtime environment, called the Java virtual machine. The specifications of
the Java language and the virtual machine are fully defined; there are
no implementation-dependent details. This architecture makes Java
an extremely portable language.

<P>
<li CLASS=listitem>Java uses a three-layer security model to protect a system from
untrusted Java code. The byte-code verifier reads byte-codes before
they are run and makes sure that they obey the basic rules of the Java
language. The class loader takes care of bringing compiled Java
classes into the runtime interpreter. The security manager handles
application-level security, by controlling whether or not a program
can access resources like the filesystem, network ports, external
processes, and the windowing system.

<P>
</UL>
<P CLASS=para>
As you can see, Java has quite a list of interesting features. If you
are a C/C++ programmer, many of the constructs of the Java language
that are covered in this book should look familiar to you. Just be
warned that you shouldn't take all of these constructs at face value,
since many of them are different in Java than they are in C/C++.

<DIV CLASS=sect1>
<h2 CLASS=sect1><A CLASS="TITLE" NAME="JLR2-CH-1-SECT-1">1.1 A "Hello World" Program</A></h2>

<P CLASS=para>
Before diving into the various constructs provided by the
Java language, you should have at least a general understanding
of the Java programming environment. In the fine tradition of all
language reference manuals, here is a short Java program that outputs
"Hello world!" and then exits:

<DIV CLASS=screen>
<P>
<PRE>
/*
 * Sample program to print "Hello World"
 */
class HelloWorld {             // Declare class HelloWorld
    public static void main(String argv[]) {
        System.out.println("Hello World!");
    }
}
</PRE>
</DIV>

<P CLASS=para>
This example begins with a comment that starts with <tt CLASS=literal>/*</tt>
and ends with <tt CLASS=literal>*/</tt>. This type of comment is called
a <I CLASS=emphasis>C-style comment</I>. The example also uses another kind of comment
that begins with <tt CLASS=literal>//</tt> and ends at the end of the
line. This kind of comment is called a <I CLASS=emphasis>single-line
comment</I>; it is identical to that style of comment
in C++. Java supports a third type of comment, called a <I CLASS=emphasis>documentation
comment</I>, that provides for the extraction of comment text into a
machine-generated document.

<P CLASS=para>
Comments aside, the example consists of a single class
declaration for the class called <tt CLASS=literal>HelloWorld</tt>.
If you are unfamiliar with classes, you can think of a class as
a collection of variables and pieces of executable code called <I CLASS=emphasis>methods</I>
for the purposes of this discussion. In Java, most executable code
is part of a method.
Methods are identical to virtual member functions in C++, except that
they can exist only as part of a class. Methods are also similar
to functions, procedures, and subroutines in other programming languages.

<P CLASS=para>
The <tt CLASS=literal>HelloWorld</tt> class contains a single
method named <tt CLASS=literal>main()</tt>.
When you ask the Java interpreter to run a Java program, you tell
it what code to run by giving it the name of a class. The Java interpreter
then loads the class and searches it for a method named
<tt CLASS=literal>main()</tt> that has the same attributes and
parameters as shown in the example. The interpreter
then calls that <tt CLASS=literal>main()</tt> method.

<P CLASS=para>
In the declaration of <tt CLASS=literal>main()</tt>, the name
<tt CLASS=literal>main</tt> is preceded by the three keywords: <tt CLASS=literal>public</tt>,
<tt CLASS=literal>static</tt>, and <tt CLASS=literal>void</tt>. The <tt CLASS=literal>public</tt>
modifier makes the <tt CLASS=literal>main()</tt>
method accessible from any class. The <tt CLASS=literal>static</tt>
modifier, when applied to a method, means that the method can be
called independently of an instance of a class. The <tt CLASS=literal>void</tt>
keyword means that the method returns no value. The <tt CLASS=literal>main()</tt>
method of an application should always be declared with these three
keywords. Although the meanings of these keywords is similar to
their meanings in C++, there are some differences in the meaning
of the keyword <tt CLASS=literal>static</tt> as used in Java and C++.

<P CLASS=para>
The <tt CLASS=literal>main()</tt> method contains a single line
of executable code that calls the <tt CLASS=literal>println()</tt>
method of the object <tt CLASS=literal>System.out</tt>. Passing the
argument <tt CLASS=literal>"Hello World!"</tt> to the <tt CLASS=literal>println()</tt>
method results in "Hello World!" being output. <tt CLASS=literal>System.out</tt> is an object that encapsulates an application's standard
output. It is similar in purpose to <tt CLASS=literal>stdout</tt> in
C and <tt CLASS=literal>cout</tt> in C++. Java also
has <tt CLASS=literal>System.in</tt>
and <tt CLASS=literal>System.err</tt> objects that are similar in purpose
to <tt CLASS=literal>stdin</tt> and <tt CLASS=literal>stderr</tt> in C and
<tt CLASS=literal>cin</tt> and <tt CLASS=literal>cerr</tt> in C++, respectively.

</DIV>


<DIV CLASS=htmlnav>

<P>
<HR align=left width=515>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch00_07.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><a href="index.htm"><img src='gifs/txthome.gif' border=0 alt='Home'></a></td>
<td width=172 align=right valign=top><A HREF="ch01_02.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
<tr>
<td width=172 align=left valign=top>Acknowledgments</td>
<td width=171 align=center valign=top><a href="index/idx_0.htm"><img src='gifs/index.gif' alt='Book Index' border=0></a></td>
<td width=172 align=right valign=top>New Language Features in Java 1.1</td>
</tr>
</table>
<hr align=left width=515>

<IMG SRC="gifs/smnavbar.gif" USEMAP="#map" BORDER=0> 
<MAP NAME="map"> 
<AREA SHAPE=RECT COORDS="0,0,108,15" HREF="../javanut/index.htm"
alt="Java in a Nutshell"> 
<AREA SHAPE=RECT COORDS="109,0,200,15" HREF="../langref/index.htm" 
alt="Java Language Reference"> 
<AREA SHAPE=RECT COORDS="203,0,290,15" HREF="../awt/index.htm" 
alt="Java AWT"> 
<AREA SHAPE=RECT COORDS="291,0,419,15" HREF="../fclass/index.htm" 
alt="Java Fundamental Classes"> 
<AREA SHAPE=RECT COORDS="421,0,514,15" HREF="../exp/index.htm" 
alt="Exploring Java"> 
</MAP>
</DIV>

</BODY>
</HTML>
